package com.leancloud.login.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.aries.ui.view.title.TitleBarView;
import com.leancloud.login.LoginPath;
import com.leancloud.login.R;
import com.leancloud.login.bean.BUser;
import com.leancloud.login.user.LoginBack;
import com.leancloud.login.user.UserGlobal;
import com.vise.xsnow.event.BusManager;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.event.LoginChangeEvent;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.ToastUtil;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

@Route(path = LoginPath.LOGIN_LOGIN)
public class LoginActivity extends BaseTitleActivity implements View.OnClickListener {
    private EditText phoneEt, passwordEt;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTextColor(Color.WHITE)
                .setLeftTextDrawable(null)
                .setTitleMainText(getResources().getString(R.string.regist_login_title))
                .setBgColor(Color.TRANSPARENT)
                .setDividerHeight(0)
                .setStatusBarLightMode(true)
                .setRightTextDrawable(R.drawable.login_icon_back)
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_phone_pwd_login;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
//        ((TextView) findViewById(R.id.input_banner_tx)).setText(getResources().getString(R.string.regist_login_title));
        phoneEt = findViewById(R.id.login_input_phone_et);
        passwordEt = findViewById(R.id.login_input_password_et);

        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.miss_password_tx).setOnClickListener(this);
        findViewById(R.id.to_registered_tx).setOnClickListener(this);
//        findViewById(R.id.phone_verify_login_tx).setOnClickListener(this);

        setToggleEffect();
    }

    private void setToggleEffect() {
        ((ToggleButton) findViewById(R.id.password_toggle))
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        setPasswordShowOrHide(passwordEt, isChecked);
                    }
                });
    }

    private void setPasswordShowOrHide(EditText editText, boolean checked) {
        if (checked) {
            editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_login) {
            doLogin();
        } else if (v.getId() == R.id.to_registered_tx) {
            ARouter.getInstance().build(LoginPath.LOGIN_REGISTER).greenChannel().navigation();
//            FastUtil.startActivity(mContext,ResetPwdActivity.class);
            finish();
        } else if (v.getId() == R.id.miss_password_tx) {
            ARouter.getInstance().build(LoginPath.LOGIN_RESETPWD).greenChannel().navigation();
        }
//        else if (v.getId() == R.id.phone_verify_login_tx) {
//            ARouter.getInstance().build(LoginPath.LOGIN_PHONE).greenChannel().navigation();
//        }
    }

    private void doLogin() {
        if (DataUtils.isEmpty(phoneEt.getText())) {
            ToastUtil.show(getResources().getString(R.string.regist_login_phone));
            return;
        }
        if (DataUtils.isEmpty(passwordEt.getText())) {
            ToastUtil.show(getResources().getString(R.string.regist_pw));
            return;
        }
        final String number = phoneEt.getText().toString().trim();
        final String password = passwordEt.getText().toString().trim();
        showLoading();

        UserGlobal.getUserImp().userLogin(number, password, "", new LoginBack() {
            @Override
            public void onBack(Object o, String err) {
                hideLoading();
                if (err == null) {
                    BusManager.getBus().post(new LoginChangeEvent(true)); //通知登录改变
                    LoginActivity.this.finish();
                } else {
                    ToastUtil.show(getResources().getString(R.string.regist_login_fail));
//                    ToastUtil.show("0");
                }
            }
        });

//        BmobUser.loginByAccount(number, password, new LogInListener<BUser>() {
//            @Override
//            public void done(BUser bUser, BmobException e) {
//                if (e == null) {
//                    Log.e("PWDebug", "三方登录成功:");
//                    UserGlobal.getUserImp().allianceLogin(bUser.getObjectId(), bUser.getMobilePhoneNumber(), new LoginBack() {
//                        @Override
//                        public void onBack(Object o, String err) {
//                            hideLoading();
//                            if (err == null) {
//                                BusManager.getBus().post(new LoginChangeEvent(true)); //通知登录改变
//                                LoginActivity.this.finish();
//                            } else {
//                                ToastUtil.show("登录失败请检查网络");
//                            }
//                        }
//                    });
//                } else {
//                    hideLoading();
//                    ToastUtil.show("手机号或密码错误");
//                }
//            }
//        });
    }
}
